﻿Imports System.Windows.Forms
Imports WebSitePanel.SkinDesigner.Business
Public Class dialSettings
    Private _designer As Designer
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.DialogResult = DialogResult.Abort
        Me.Close()
    End Sub
    Private Sub btnNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNewTheme.Clicked
        If dialThemeName.ShowDialog = Windows.Forms.DialogResult.OK Then
            If sfdSkin.ShowDialog = Windows.Forms.DialogResult.OK Then
                ThemeManager.CreateTheme(sfdSkin.FileName, dialThemeName.ThemeName)
                _designer = New Designer(sfdSkin.FileName, dialThemeName.ThemeName)
                Toolbox.AddRecentTheme(dialThemeName.ThemeName, sfdSkin.FileName)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()
            End If
        End If
    End Sub
    Public ReadOnly Property Designer As Designer
        Get
            Return _designer
        End Get
    End Property

    Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Clicked
        If Me.ofdSkin.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim themename As String = ThemeManager.LoadTheme(ofdSkin.FileName)
            _designer = New Designer(ofdSkin.FileName, dialThemeName.ThemeName)
            Toolbox.AddRecentTheme(themename, ofdSkin.FileName)
            Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()
        End If
    End Sub

    Private Sub dialSettings_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For Each item As KeyValuePair(Of String, String) In Toolbox.GetRecentThemes.Reverse
            Dim b As New ucButton
            b.ButtonText = item.Key
            b.Tag = item.Value
            b.Margin = New Padding(0)
            Me.flpRecentItems.Controls.Add(b)
            AddHandler b.Clicked, AddressOf RecentTheme_Clicked
            AddHandler b.Deleted, AddressOf RecentTheme_Deleted
        Next
    End Sub
    Public Sub RecentTheme_Clicked(ByVal Sender As Object, ByVal e As EventArgs)
        Dim path As String = CType(Sender, ucButton).Tag
        Dim themename As String = ThemeManager.LoadTheme(path)
        _designer = New Designer(path, themename)
        Toolbox.AddRecentTheme(themename, path)
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub
    Public Sub RecentTheme_Deleted(ByVal sender As Object, ByVal e As EventArgs)
        Toolbox.RemoveRecentTheme(CType(sender, ucButton).ButtonText, CType(sender, ucButton).Tag)
        Me.flpRecentItems.Controls.Remove(sender)
    End Sub
End Class